/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bo;

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.*;

/**
 *
 * @author amine.tayaa
 */
public class RS232 {

    void connect ( String portName ) throws Exception
	    {
	        CommPortIdentifier COM4 = CommPortIdentifier.getPortIdentifier(portName);
	        // si le port est deja connecté:
	        if ( COM4.isCurrentlyOwned() )
	        {
	            System.out.println("Error: Port occupe");
	        }
	        else
	        {
	            CommPort commPort = COM4.open(this.getClass().getName(),2000);
	            if ( commPort instanceof SerialPort )
	            {
	            	//si le port est présent mais pas connecté
	                SerialPort serialPort = (SerialPort) commPort;
	                serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);

	                InputStream in = serialPort.getInputStream();
	                OutputStream out = serialPort.getOutputStream();

                        System.out.println(in);
                                
//	                (new Thread(new SerialReader(in))).start();
//	                (new Thread(new SerialWriter(out))).start();
                     
                        

	            }
	            else
	            	// si le port n'est pas présent
	            {
	                System.out.println("Error: Le port n'est pas present");
	            }
	        }
         }
}